import React from "react";

//父组件
class Parent extends React.Component {
    // 父传子
    // state = {lastName: "MangoPie"}
    // render() {
    //     return (
    //         <div>
    //             传递子组件的数据:<Child name={this.state.lastName}/>
    //         </div>
    //     )
    // }

    //子传父
    getChildMsg = (msg) => {
        console.log("接收到子组件传来的数据" + msg);
    }

    render() {
        return (
            <div>子组件:<Child getMsg={this.getChildMsg}/></div>
        )
    }
}

class Child extends React.Component {
    state = {childMsg: "MangoPie"};
    handleClick = () =>{
        this.props.getMsg(this.state.childMsg)
    }
    render() {
        return(
            <button onClick={this.handleClick}>点击给父组件传数据</button>
        )
    }
}



// function Child(props) {
//     // 父传子
//     return (
//         <div>子组件接收到数据:{props.name}</div>
//     )
// }

export default Parent;




